package com.tju.resource.business.tb_qc_order;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tju.resource.mapper.TbProdPlanMapper;
import com.tju.resource.mapper.TbQcOrderMapper;
import com.tju.resource.mapper.TbReportOrderMapper;
import com.tju.resource.model.TbProdPlan;
import com.tju.resource.model.TbQcOrder;
import com.tju.resource.model.TbReportOrder;
import com.tju.resource.business.tb_order.TbOrderService;
import com.tju.resource.business.tb_prod_plan.TbProdPlanService;
import com.tju.resource.business.tb_report_order.TbReportOrderService;
import com.tju.resource.result.Result;
import com.tju.resource.result.ResultEnum;
import com.tju.resource.result.ResultUtil;
import com.tju.resource.tool.tool.QuickQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Slf4j
@Service
public class TbQcOrderServiceImpl implements TbQcOrderService {

    @Autowired
    TbQcOrderMapper mapper;

    @Autowired
    TbProdPlanMapper ppMapper;

    @Autowired
    TbReportOrderMapper repMapper;

    @Autowired
    TbProdPlanService ppService;

    @Autowired
    TbReportOrderService trService;
    @Autowired
    private TbOrderService orderService;

    @Override
    public Result<Object> getUnApp(Map<String, String> params) {
        try {
            Page<TbQcOrder> page = new Page<>(Long.parseLong(params.get("page")), Long.parseLong(params.get("limit")));
            IPage<TbQcOrder> iPage = mapper.getUnApp(page, params);
            List<TbQcOrder> records = iPage.getRecords();

            return ResultUtil.success(ResultEnum.SUCCESS, records, iPage.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, e.getMessage());
        }
    }

    @Override
    public Result<Object> getAppLog(Map<String, String> params) {
        try {
            Page<TbQcOrder> page = new Page<>(Long.parseLong(params.get("page")), Long.parseLong(params.get("limit")));
            IPage<TbQcOrder> iPage = mapper.getAppLog(page, params);
            List<TbQcOrder> records = iPage.getRecords();

            return ResultUtil.success(ResultEnum.SUCCESS, records, iPage.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, e.getMessage());
        }
    }

    @Override
    public Result<Object> add(TbQcOrder qco) {
        Result<Object> ret = new Result<>();

        try {
            if (!StringUtils.isBlank(qco.getFromDiv())) {
                int insert = mapper.insert(qco);

                if (insert > 0) {
                    ret.setCode(ResultEnum.SUCCESS.getCode());
                    ret.setMsg("新增转序单成功!");
                } else {
                    ret.setCode(ResultEnum.UNKONW_ERROR.getCode());
                    ret.setMsg("新增转序单失败!");
                }
                ret.setData(insert);
            } else {
                ret.setCode(ResultEnum.UNKONW_ERROR.getCode());
                ret.setMsg("转序单来源不可为空!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            ret.setCode(ResultEnum.UNKONW_ERROR.getCode());
            ret.setMsg(e.getMessage());
        }

        return ret;
    }

    @Override
    public Result<Object> upd(TbQcOrder qco) {
        Result<Object> ret = new Result<>();

        try {
            TbQcOrder order = QuickQuery.byOne(mapper, "id", qco.getId());

            if (null != order) {
                if (StringUtils.equals(qco.getFinished(), order.getFinished()) || StringUtils.isBlank(qco.getFinished())) {
                    int update = mapper.updateById(qco);

                    if (update > 0) {
                        ret.setCode(ResultEnum.SUCCESS.getCode());
                        ret.setMsg("修改转序单成功!");
                    } else {
                        ret.setCode(ResultEnum.UNKONW_ERROR.getCode());
                        ret.setMsg("修改转序单失败!");
                    }
                    ret.setData(update);
                } else {
                    ret.setCode(ResultEnum.UNKONW_ERROR.getCode());
                    ret.setMsg("修改转序单不可修改状态!");
                }

            } else {
                ret.setCode(ResultEnum.UNKONW_ERROR.getCode());
                ret.setMsg("转序单不存在!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            ret.setCode(ResultEnum.UNKONW_ERROR.getCode());
            ret.setMsg(e.getMessage());
        }

        return ret;
    }

    @Override
    @Transactional
    public Result<Object> app(TbQcOrder qco) {
        Result<Object> ret = new Result<>();
        Long repId = qco.getId();
        TbReportOrder tr = new TbReportOrder();
        TbReportOrder repo = QuickQuery.byOne(repMapper, "id", qco.getId());
        List<TbProdPlan> tbProdPlan = ppMapper.TbProdPlanById(qco.getPpId());
        TbProdPlan prodPlan = new TbProdPlan();
        TbReportOrder reportOrder = repMapper.selectById(repId);


        qco.setId(null);
        tr.setParentId(reportOrder.getParentId());
        tr.setId(repId);
        tr.setReportBy(qco.getAppBy());
        tr.setReportQty(qco.getQualify());
        tr.setPpId(qco.getPpId());
        tr.setOutwardId(qco.getOutwardId());
        tr.setRemark(qco.getRemark());
        prodPlan.setId(tr.getPpId());
        prodPlan.setFpNumber(Long.parseLong(qco.getQualify() + ""));
        prodPlan.setSpNumber(Long.parseLong(qco.getDisQua() + ""));
        prodPlan.setRemark(qco.getRemark());
        prodPlan.setCreateBy(qco.getAppBy());

        if (null != repo) {
            tr.setOnFrom(repo.getToWhere());
            if (repo.getCurStep() != tbProdPlan.size() - 1) {
                tr.setToWhere(tbProdPlan.get(repo.getCurStep()).getProcessCode());
            } else {
                tr.setToWhere(null);
            }
            tr.setCurStep(repo.getCurStep());
        } else {
            ret.setCode(ResultEnum.UNKONW_ERROR.getCode());
            ret.setMsg("审核失败,无法获取工序流程!");
            return ret;
        }


        Result<Object> repRes = trService.insertTbReportOrder(tr);

        if (repRes.getCode().equals(ResultEnum.SUCCESS.getCode())
                /*&& updMatl.getCode().equals(ResultEnum.SUCCESS.getCode())
                && result*/) {
            qco.setRepId(Long.parseLong(repRes.getData() + ""));

            int insert = mapper.insert(qco);
            if (insert > 0) {
                return ResultUtil.success(ResultEnum.SUCCESS, "审核完成!");
            } else {
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "审核失败!");
            }
//        Result<Object> updMatl = ppService.updateFinCount(prodPlan);
//        boolean result = orderService.updateOrdMatl(qco.getOrderNumber(), qco.getMatlNumber());

        } else {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "审核失败!");
        }
    }

    @Override
    public Result<Object> del(TbQcOrder qco) {
        Result<Object> ret = new Result<>();

        try {
            if (null != QuickQuery.byOne(mapper, "id", qco.getId())) {
                int delete = mapper.deleteById(qco.getId());

                if (delete > 0) {
                    ret.setCode(ResultEnum.SUCCESS.getCode());
                    ret.setMsg("删除转序单成功!");
                } else {
                    ret.setCode(ResultEnum.UNKONW_ERROR.getCode());
                    ret.setMsg("删除转序单失败!");
                }
            } else {
                ret.setCode(ResultEnum.UNKONW_ERROR.getCode());
                ret.setMsg("转序单不存在!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            ret.setCode(ResultEnum.UNKONW_ERROR.getCode());
            ret.setMsg(e.getMessage());
        }

        return ret;
    }

    @Override
    public TbQcOrder byId(Long id) {
        return QuickQuery.byOne(mapper, "id", id);
    }

    @Override
    public List<TbQcOrder> byRep(Long repId) {
        return QuickQuery.list(mapper, "rep_id", repId.toString());
    }
}
